package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class EditAccountServlet extends HttpServlet {
    private String msg;
    private static  final  String EDIT_Account_FORM = "/WEB-INF/jsp/account/editAccount.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("account");
        if(account != null) {
            String firstName = req.getParameter("account.firstName");
            String lastName = req.getParameter("account.lastName");
            String email = req.getParameter("account.email");
            String phone = req.getParameter("account.phone");
            String address1 = req.getParameter("account.address1");
            String address2 = req.getParameter("account.address2");
            String city = req.getParameter("account.city");
            String state = req.getParameter("account.state");
            String zip = req.getParameter("account.zip");
            String country = req.getParameter("account.country");
            String language = req.getParameter("account.languagePreference");
            String favourite = req.getParameter("account.favouriteCategoryId");
            String password = req.getParameter("password");
            String repeatedPassword = req.getParameter("repeatedPassword");
            String bannerOptionStr = req.getParameter("account.bannerOption");
            String listOptionStr = req.getParameter("account.listOption");
            boolean bannerOption = "on".equalsIgnoreCase(bannerOptionStr);
            boolean listOption = "on".equalsIgnoreCase(listOptionStr);
            String bannerName = "<image src=\"images/banner_" + favourite.toLowerCase() + ".gif\">";

            if(!password.equals("") && !repeatedPassword.equals("")){
                if (!password.equals(repeatedPassword)) {
                    this.msg = "密码错误";
                    req.setAttribute("msg", this.msg);
                    req.getRequestDispatcher(EDIT_Account_FORM).forward(req,resp);
                }else{
                    //更改密码
                    account.setPassword(password);
                }
            }

            if(firstName.isEmpty()||lastName.isEmpty()||email.isEmpty()||phone.isEmpty()||address1.isEmpty()
                    ||address2.isEmpty()||city.isEmpty()||state.isEmpty()||zip.isEmpty()||country.isEmpty()){
                this.msg = "输入不能为空";
                req.setAttribute("msg", this.msg);
                req.getRequestDispatcher(EDIT_Account_FORM).forward(req,resp);
            }else{
                //更新account对象
                account.setFirstName(firstName);
                account.setLastName(lastName);
                account.setEmail(email);
                account.setPhone(phone);
                account.setAddress1(address1);
                account.setAddress2(address2);
                account.setCity(city);
                account.setState(state);
                account.setZip(zip);
                account.setCountry(country);
                account.setLanguagePreference(language);
                account.setFavouriteCategoryId(favourite);
                account.setBannerOption(bannerOption);
                account.setListOption(listOption);
                account.setBannerName(bannerName);

                CatalogService catalogService = new CatalogService();
                List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
                session.setAttribute("myList", myList);

                //更新数据库
                AccountService accountService = new AccountService();
                accountService.updateAccount(account);
                //session中loginAccount也更新
                session.setAttribute("loginAccount", account);
                req.getRequestDispatcher(EDIT_Account_FORM).forward(req,resp);
            }
        }
    }
}
